import moment from 'moment'
// 日期格式化 --- 年月日时分秒
export function formatDateTime (value) {
    let date = new Date(value)
    let y = date.getFullYear()
    let MM = date.getMonth() + 1
    MM = MM < 10 ? '0' + MM : MM
    let d = date.getDate()
    d = d < 10 ? '0' + d : d
    let h = date.getHours()
    h = h < 10 ? '0' + h : h
    let m = date.getMinutes()
    m = m < 10 ? '0' + m : m
    let s = date.getSeconds()
    s = s < 10 ? '0' + s : s
    return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
}

export function formatDate (value) {
  let date = new Date(value);
  let y = date.getFullYear();
  let MM = date.getMonth() + 1;
  MM = MM < 10 ? ('0' + MM) : MM;
  let d = date.getDate();
  d = d < 10 ? ('0' + d) : d;
  return y + '-' + MM + '-' + d;
}

export function getDateDiff (timeStr) {
    let result = ''
    let now = new Date().getTime()
    let diffValue = now - new Date(timeStr).getTime()
    let minute = 1000 * 60
    let hour = minute * 60
    let day = hour * 24
    let month = day * 30
    let year = month * 12

    let _year = diffValue / year
    let _month = diffValue / month
    let _week = diffValue / (7 * day)
    let _day = diffValue / day
    let _hour = diffValue / hour
    let _min = diffValue / minute

    if (diffValue < 0) {
        return
    }
    if (_year >= 1) result = parseInt(_year) + '年前'
    else if (_month >= 1) result = moment(timeStr).format('M月D日')
    else if (_week >= 1) result = moment(timeStr).format('M月D日')
    else if (_day >= 1) result = moment(timeStr).format('M月D日')
    else if (_hour >= 1) result = parseInt(_hour) + '小时'
    else if (_min >= 1) result = parseInt(_min) + '分钟前'
    else result = '刚刚'
    return result
}
export function formatUsDate (timeStr) {
    return moment().format('LLL')
}
